<?php
/* 
 *收货列表
 */
class ReceiveAction extends CommonAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['Buy.mano'] = array('like',"%".$key."%");
            $where['Product.name'] = array('like',"%".$key."%");
            $where['Product.ename'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");
          
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if( $_REQUEST['status'] != ''  )
            $map['status'] = $_REQUEST['status'];
        else
            $map['status'] = array('gt',1); //大于1的状态才显示在收货列表中

        if( !empty($_REQUEST['supplier']) )
            $map['Supplier.name'] = array('like',"%".$_REQUEST['supplier']."%");

        if( !empty($_REQUEST['order_detail_mano']) )
            $map['Order_detail.mano'] = array('like',"%".$_REQUEST['order_detail_mano']."%");

    }

    public function index(){
        if( !empty($_REQUEST['order_detail_mano']) )
            parent::index('BuyOrderView');
        else if(!empty($_REQUEST['supplier']))
            parent::index('BuySPView');
        else
            parent::index('BuyView');
    }

    public function edit(){
        $model = M ( 'Buy');
        $id = $_REQUEST [$model->getPk ()];
        $vo = $model->getById ( $id );
        $this->assign ( 'vo', $vo );
        $this->display ();
    }

    //显示货前样已送检的采购单
    public function sample_test_loginfo()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $list = M('Buy') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的采购单不存在！');
            $this->assign('receives',$list);
            $this->display();
        }else
          $this->error("请选择采购单！");
    }


     //显示货物送送检的采购单
    public function goods_test_loginfo()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $list = M('Buy') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的采购单不存在！');
            $this->assign('receives',$list);
            $this->display();
        }else
          $this->error("请选择采购单！");
    }

    //样品收到页面
    public function sample_got()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $list = M('Buy') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的采购单不存在！');
            $this->assign('receives',$list);
            $this->display();
        }else
          $this->error("请选择采购单！");
    }

    //入库处理，如果检测合格或免检了才能入库
    public function in_house()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $condition['in_qc_status'] = array('in','1,3'); //货物免检或已经检测合格
            $condition['status'] = array('lt',10); //小于10的才能入库：拒绝入库的可以再重新入库
            $condition['receive_number'] = array('gt',0); //入库数量必须大于0
            $list = M('Buy') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的采购单不合法：可能货物质检尚未通过；或已经入库；或入库数量 为0！');
            $data = array(
                'status' => 8,
                );
            //采购单状态变为：入库
            $rs = M('Buy') ->where ( $condition )->save($data); //设置为入库

            if($rs){
                foreach ($list as  $v) {
                    //销售分单状态要变为到货通知
                    if($v['order_detail_id'])
                        M('Order_detail')->where('id = '.$v['order_detail_id'])->setField('status',3);
                    //异常管理
                    if($v['exception_status'] == 2 || $v['exception_status'] == 4  ){//退货或退货纯化
                        $v['exception_status'] == $v['exception_status'] + 1; //变为合格
                        M('Buy')->where('id = '.$v['id'])->setField('exception_status',$v['exception_status']);
                    }
                    //入库增库存
                   A('Trans')->updateStock($v['product_id'],$v['specification'],'in',$v['receive_number']);
                }
            
           
                $this->success("批量入库成功");
            }
            else
                $this->error("批量入库失败");

        }else
          $this->error("请选择采购单！");
    }

    //拒绝入库处理，如果检测合格或免检了才能入库
    public function no_house()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $condition['in_qc_status'] = 4; //货物检测不合格
            $list = M('Buy') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的采购单不合法！');
            $rs = M('Buy') ->where ( $condition )->setField('status',9); //设置为拒绝入库
            if($rs){
                //异常管理
                $rs = M('Buy') ->where ( $condition )->setField('exception_status',1); //设置为待处理退货
                $this->success("拒绝入库成功");
            }else
                $this->error("拒绝入库失败");

        }else
          $this->error("请选择采购单！");
    }

    //适用于批量送检物流 信息更新
    public function mass_update()
    {
        A('Buy')->mass_update();
    }

    public function update()
    {
        A('Buy')->update();
    }

}
?>
